package com.woniuxy.yogaapp.mapper;

import com.woniuxy.yogaapp.pojo.Chat;

import com.woniuxy.yogaapp.provider.ChatProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 操作chat表
 */
public interface ChatMapper {

	/**
	 * 发送给管理员认证信息
	 * 
	 * @param chat
	 * @return
	 */
	@Insert("insert into chat(sender,recipient,mes,state,mestime,mestype,deal) values (#{sender},#{recipient},#{mes},#{state},#{mestime},#{mestype},#{deal})")
	int addChat1(Chat chat);

	/**
	 * 添加通知信息
	 *
	 * @param chat
	 * @return
	 */
	@Insert("insert into chat(sender,recipient,mes,state,mestime,mestype) values (#{sender},#{recipient},#{mes},#{state},#{mestime},#{mestype})")
	@Options(useGeneratedKeys = true, keyProperty = "chatid", keyColumn = "chatid")
	int addChat(Chat chat);

	/**
	 * 根据id查询发的说说
	 *
	 * @param uid
	 * @return
	 */
	@Select("select * from chat where sender = #{uid} and mestype = 5")
	@Results({
			@Result(column = "chatid", property = "images", many = @Many(select = "com.woniuxy.yogaapp.mapper.ChatImageMapper.findChatimageBychatid")) })
	List<Chat> findSayChatByUid(Integer uid);

	/**
	 * 删除chat中的通知
	 *
	 * @param chatid
	 * @return
	 */
	@Delete("delete from chat where chatid = #{chatid}")
	int deleteChat(Integer chatid);

	/**
	 * 根据双方id查询私聊的信息
	 * 
	 * @param meUid
	 *            用户的id
	 * @param otherUid
	 *            好友的id
	 * @return
	 */
	@Select("select * from chat where ((sender = #{param1} and recipient = #{param2}) or (sender = #{param2} and recipient = #{param1})) and mestype = 1;")
	List<Chat> findPrivateChat(Integer meUid, Integer otherUid);

	/**
	 * 动态查询通知
	 *
	 * @param chat
	 * @return
	 */
	@SelectProvider(type = ChatProvider.class, method = "findChat")
	List<Chat> findChat(Chat chat);

	/**
	 * 动态修改信息的状态（state）
	 *
	 * @param chat
	 */
	@UpdateProvider(type = ChatProvider.class, method = "updateChat")
	void updateChat(Chat chat);

	/**
	 * 更改教练认证消息为已读
	 * 
	 * @param chat
	 * @return
	 */
	@Update("update chat set state=#{state} where chatid=#{chatid}")
	int changeState(Chat chat);

	/**
	 * 更改认证消息为已读
	 * 
	 * @param chatid
	 * @return
	 */
	@Update("update chat set state='已读' where chatid=#{chatid}")
	int changeStatetoReady(int chatid);

	// 更改处理状态为驳回
	@Update("update chat set deal='驳回' where chatid=#{chatid}")
	int changeDealDisAgree(int chatid);

	// 更改处理状态为同意
	@Update("update chat set deal='同意' where chatid=#{chatid}")
	int changeDealAgree(int chatid);

	// 查询所有的未处理教练认证请求
	@Select("select * from chat where mestype=6 and deal='未处理'")
	List<Chat> findCoachChat();

	// 查询所有的未处理场馆认证请求
	@Select("select * from chat where mestype=7 and deal='未处理'")
	List<Chat> findClubChat();

	// 查询所有的已处理教练认证请求
	@Select("select * from chat where mestype=6 and deal in('同意','驳回')")
	List<Chat> findReadyCoachChat();

	// 查询所有的已处理场馆认证请求
	@Select("select * from chat where mestype=7 and deal in('同意','驳回')")
	List<Chat> findReadyClubChat();

	/**
	 * 动态查询未读信息
	 *
	 * @param chat
	 * @return
	 */
	@SelectProvider(type = ChatProvider.class, method = "findUnreadInfo")
	Integer findUnreadInfo(Chat chat);

	/**
	 * 改变教练向场馆发送请求为 已读
	 * 
	 * @param chat
	 * @return
	 */
	@Update("update chat set state=#{state},mestype=2 where sender=#{sender} and recipient=#{recipient}")
	int changeState9(Chat chat);

	/**
	 * 修改除私聊以外的通知的状态
	 * 
	 * @param uid
	 */
	@Update("update chat set state = '已读' where recipient = #{uid} and mestype not in (1)")
	void updateOtherChat(Integer uid);

	/**
	 * 查找特点好友的私聊未读信息
	 * 
	 * @param me
	 * @param other
	 * @return
	 */
	@Select("select count(*) from chat where sender = #{param1} and recipient = #{param2} and state = '未读' and mestype=1")
	Integer findUnreadeByUid(Integer me, Integer other);

	/**
	 * 查找咨询关系的私聊未读信息
	 * 
	 * @param me
	 * @param other
	 * @return
	 */
	@Select("select count(*) from chat where sender = #{param1} and recipient = #{param2} and state = '未读' and mestype=12")
	Integer findUnreadeByUid1(Integer me, Integer other);

	/**
	 * 修改私聊信息已读为未读
	 * 
	 * @param chat
	 */
	@Update("update chat set state = '已读' where sender = #{sender} and recipient = #{recipient} and state = '未读' and mestype=1")
	void updatePrivateChat(Chat chat);

	/**
	 * 根据双方id查询私聊的信息
	 * 
	 * @param meUid
	 *            用户的id
	 * @param otherUid
	 *            好友的id
	 * @return
	 */
	@Select("select * from chat where ((sender = #{param1} and recipient = #{param2}) or (sender = #{param2} and recipient = #{param1})) and mestype = 12;")
	List<Chat> findConsultationChat(Integer meUid, Integer otherUid);

	/**
	 * 修改咨询信息已读为未读
	 * 
	 * @param chat
	 */
	@Update("update chat set state = '已读' where sender = #{sender} and recipient = #{recipient} and state = '未读' and mestype=12")
	void updateConsultationChat(Chat chat);
}
